﻿using System;
using cfg.enemy;
using QAssetBundle;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.U2D;

namespace QFramework.Monk
{
    public class EnemyBase:GameEntityObj,IController
    {
        public float mHealth = 3;
        
        protected float mCurTime = 0;
        protected float mSpeed = 2;
        protected EnemyInfo mInfo;
        protected bool mHasHurt = false;
        protected bool mIsHitBack = false;//击退
        protected float mCurHitBackTime = 0;//击退时间
        
        public SpriteRenderer mSprite;
        public CircleCollider2D mHitBox;
        public Rigidbody2D selfRigidbody2D;
        
        protected GenerateItemSystem mGenerateItemSystem;
        protected virtual void Awake()
        {
            mGenerateItemSystem = this.GetSystem<GenerateItemSystem>();
            if (mSprite==null)
            {
                mSprite = this.GetComponentInChildren<SpriteRenderer>();
            }

            if (mHitBox==null)
            {
                mHitBox = this.GetComponentInChildren<CircleCollider2D>();
                
            }

            if (selfRigidbody2D==null)
            {
                selfRigidbody2D = this.GetComponent<Rigidbody2D>();
                
            }
            
        }
        protected virtual void Start()
        {
            AppStart.enemyNum.Value++;
            
        }
        
        protected virtual void OnDestroy()
        {
            AppStart.enemyNum.Value--;
        }
        
        protected virtual void Update()
        {
            if (mIsHitBack)
            {
                mCurHitBackTime+= Time.deltaTime;
                if (mCurHitBackTime>0.3)
                {
                    mIsHitBack = false;
                    mCurHitBackTime = 0;
                }
            }
            else
            {
                Move();
                
            }
        }
        protected virtual void FixedUpdate()
        {
        }


        protected virtual void SetSpriteColor(Color color)
        {
            if (mSprite!=null)
            {
                mSprite.color = color;
                
            }
        }

        protected virtual void Move()
        {
           
        }

        public virtual void HitBack(Vector2 dir)
        {
            if (mInfo.CanHitBack)
            {
                mIsHitBack = true;
                //collider为触发器的前提 实现击退效果
                selfRigidbody2D.AddForce(dir,ForceMode2D.Impulse);
                
            }
        }
        
        public virtual void CostHp(float damage)
        {
            if (!mHasHurt&&mHealth>0)
            {
                mHasHurt = true;
                SetSpriteColor(Color.red);
                mHealth-=damage;
                AudioKit.PlaySound("hit");
                FlyTextCanvas.Default.CreateFlyText(damage.ToString(), transform.position);
                if (mHealth<=0)
                {
                    //取出配置中可掉落得到道具
                    if (mInfo!=null)
                    {
                        foreach (var item in mInfo.DropItems_Ref)
                        {
                            mGenerateItemSystem.GenerateItem(transform.Position(), item);
                        }
                    }
                    AudioKit.PlaySound(Sxf.ENEMY_DIE);
                    FxController.Default.Play(mSprite, Color.yellow);
                    this.gameObject.DestroySelfGracefully();
                }
                else
                {
                    ActionKit.Delay(0.15f, () =>
                    {
                        mHasHurt = false;
                        SetSpriteColor(Color.white);
                    }).Start(this);
                }
            }
        }
        
        public IArchitecture GetArchitecture()
        {
            return AppStart.Interface;
        }
        
        //初始化数值
        public virtual void InitInfo(EnemyInfo info,int hpBouns)
        {
            this.mInfo = info;
            mHealth = info.Health*hpBouns;
            mSpeed = info.Speed;
            //var spriteAtlas = mResLoader.LoadSync<SpriteAtlas>("mini_boss");
            //mSprite.sprite = spriteAtlas.GetSprite("mini_boss");
            //mSprite.sprite = Resources.Load<Sprite>(info.Sprite);
        }
        
        
        
    }
}